package com.ciwong.epaper.modules.me.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.CatalogueUnitBean;
import com.ciwong.epaper.widget.catalog.base.BaseNode;

import java.util.ArrayList;
import java.util.List;

import static android.support.v7.widget.RecyclerView.Adapter;
import static android.support.v7.widget.RecyclerView.OnClickListener;
import static android.support.v7.widget.RecyclerView.ViewHolder;

public class CatalogDialogAdapter extends Adapter {

    private Context mContext;
    private LayoutInflater factory;
    private static final int FIRST_LEVEL_PADDING = 18;
    private static final int SECOND_LEVEL_PADDING = 30;
    private static final int THIRD_LEVEL_PADDING = 36;
    private static final int ITEM_HEIGHT_ONE = 49;
    private static final int ITEM_HEIGHT_TOW = 64;
    private List<BaseNode> list = new ArrayList<>();

    public CatalogDialogAdapter(Context context) {
        this.mContext = context;
        factory = LayoutInflater.from(context);
    }

    public void setData(List<BaseNode> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {

        void onItemClick(ViewHolder holder, List<BaseNode> list, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewHolder viewHolder = null;
        View inflate = factory.inflate(R.layout.item_dialog_catalog, parent, false);
        viewHolder = new CalatogViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final BaseNode baseNode = list.get(position);
        CatalogueUnitBean catalogueUnitBean = (CatalogueUnitBean) baseNode.getNodeContent();
        TextView tvCatalog = ((CalatogViewHolder) holder).tvCatalog;
        if (baseNode.getSelfLevel() == 1) {
            tvCatalog.setPadding(dpToPixel(FIRST_LEVEL_PADDING, mContext), 0, 0, 0);
        } else if (baseNode.getSelfLevel() == 2) {
            tvCatalog.setPadding(dpToPixel(SECOND_LEVEL_PADDING, mContext), 0, 0, 0);
        } else if (baseNode.getSelfLevel() == 3) {
            tvCatalog.setPadding(dpToPixel(THIRD_LEVEL_PADDING, mContext), 0, 0, 0);
        }

        if (baseNode.isLastLevel()) {
            tvCatalog.setText("· " + catalogueUnitBean.getName());
            tvCatalog.setTextColor(mContext.getResources().getColor(R.color.color_515854));
            tvCatalog.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPixel(ITEM_HEIGHT_TOW, mContext)));
            tvCatalog.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null)
                        onItemClickListener.onItemClick(holder, list, position);
                }
            });
        } else {
            tvCatalog.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dpToPixel(ITEM_HEIGHT_ONE, mContext)));
            tvCatalog.setText(catalogueUnitBean.getName());
            tvCatalog.setTextColor(mContext.getResources().getColor(R.color.color_AAB2AE));

        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class CalatogViewHolder extends RecyclerView.ViewHolder {
        TextView tvCatalog;

        public CalatogViewHolder(View itemView) {
            super(itemView);
            tvCatalog = (TextView) itemView;
        }
    }

    int dpToPixel(float dp, Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return (int) (metrics.density * dp);
    }
}
